<?php
namespace Admin\Controller;
use Think\Controller;
class MoodController extends Controller {
    /**
     * 心情首页
     */
    public function index(){
        $moodList = D('Mood')->selectMood();
        $temp = array();
        foreach ($moodList as $key => $value) {
           $month = date('Y',$value['chat_time']);
           $temp[$month][] = $value;
        }
        $this->assign('moodList',$temp);
        $this->display('mood');
    }

    /**
     * 添加心情
     */
    public function moodAdd(){
    	$moodData = I('post.');
        $moodData['chat_time'] = time();
        $moodData['admin_user_admin_id'] = 1;
        if (empty($moodData['chat_content'])) {
            $this->error('心情内容不能为空');
        }
        if (D('Mood')->insertMood($moodData)) {
            $this->success('心情添加成功');
        } else {
            $this->error('心情添加失败');
        }
    }

    /**
     * 编辑心情
     */
    public function moodEditor(){
        if (IS_AJAX) {
           $moodId = I('get.mid');
           $moodList = D('Mood')->findMood(['chat_id' => $moodId]);
           $moodList = json_encode($moodList);
           exit($moodList);
        }

        if (IS_POST) {
           $moodData = I('post.');
           $moodData['is_display'] = array_key_exists('is_display',$moodData) ? 0 : 1;
           if (empty($moodData['chat_content'])) {
                $this->error('心情内容不能为空');
            }
            $fieldString = 'chat_title,is_display,chat_content';
            $wheres['chat_id'] = $moodData['mood_id'];
            $id = D('Mood')->updateMood($wheres,$fieldString,$moodData);
            if ($id) {
                $this->success('博文修改成功');
            } else {
                $this->error('博文修改失败');
            }
        }
    }

    /**
     * 删除心情
     */
    public function moodDel(){
        $moodId = I('get.mid');
        $id = D('Mood')->deleteArticle(['chat_id' => $moodId]);
        if ($id) {
            $this->success('博文删除成功');
        } else {
            $this->error('博文删除失败');
        }
    }
}